<?php

require_once 'VisTesting/VisSeleniumSession.php';
require_once 'VisTesting/VisSeleniumTestSuite.php';
require_once 'VisTesting/VisSeleniumXml.php';

require_once 'BaseController.php';

class SeleniumtestsController extends BaseController
{

    function init() {
        parent::init();
    }
    //=============================================================================================
    
    function indexAction() {
        echo 'SeleniumTest::indexAction()';
    }
    //=============================================================================================
    
    function runAction($project, $testSuite) {
        /*
         * Start Selenium Server
         */
        $this->_statrtSeleniumServer();
        
        /*
         * URL for testing
         * http://www.vistesting.fb/seleniumtests/run?project=SimpleTest&testSuite=LoginDefaultAdminEnglish
         */
        $testPathXml = realpath(sprintf("%s/../data/tests/%s/%s.xml", APPLICATION_PATH, $project, $testSuite));
        
        /*
         * Run Selenium Test
         */
        ob_start();
        //require $testPath;
        /* */
        if(! defined('RUNNER')) {
            define('RUNNER', true);
            
            $selSession = new VisSeleniumSession("*firefox /usr/lib/firefox-3.0.15/firefox", "http://ewshop.fb");
            
            $test = new VisSeleniumTestSuite('Test With Xml Test Cases');
            $oXml = new VisSeleniumXml($testPathXml);
            $test->addXml($oXml);
            
            $reporter = new HtmlReporter();    // 'utf8' - Nqkav Error
            $test->run($reporter);
        }
        
        /*
         * Assign Report to View
         */
        $this->view->testReport = ob_get_clean();
        
        //Render the action's own script
        $this->render('run','default');
    }
    //=============================================================================================
    
    private function _statrtSeleniumServer() {
        $seleniumServer = shell_exec('ps ax | grep selenium');
        if(stripos($seleniumServer, 'java -jar selenium-server') === FALSE) {
            $javaVersion = shell_exec('java -version 2>&1');
            if(stripos($javaVersion, 'java version') !== 0) {
            	throw Exception('You have not Java Virtual Machine.');
            }
            
            $selenimServerPath = realpath(sprintf("%s/../library/Testing/selenium-server-ff3.jar", APPLICATION_PATH));
            exec("java -jar " . $selenimServerPath . " >/dev/null &");
        }
    }
    //=============================================================================================
    
}

